<?php
namespace app\web\controller;

use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Yxgifts as Yxgifts_mod;

class Yxgifts extends Base
{
    public function Yxgifts()
    {
/**
 * Author: cbw
 * Desc: 摇一摇多产品web后台
 * Time: 2022/3/30 17:29
 * Email: 17854288794@163.com
 */
global $_G,$_P;
$params = $_P;
$themeid = isset($params['themeid'])?$params['themeid']:'';//活动id
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
$config =   Theme::getConfig($themeid);
$themelist = Theme::getAllTheme();
$system= $_G['system'];
$do = 'yxgifts';
$op = !empty($params['op'])?$params['op']:'display';

//业务参数
$search_data = isset($params['search_data'])?$params['search_data']:'';
$status  = isset($params['status'])?$params['status']:0;
$page_index = max(1,intval($params['page']));
$page_size = 50;
$page_start = ($page_index-1)*$page_size;

$gifts = Yxgifts_mod::getFormatGifts($config);

$status_arr = Yxgifts_mod::getStatusArr($themeid);
if ($op=='display'){
    if (empty($_P['createtime']['start'])){
        $start_day =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
        $start_day =  strtotime(date('Y-m-d 00:00:00',$start_day));
    }else{
        $start_day =  strtotime($_P['createtime']['start']);
    }
    $end_day = empty($_P['createtime']['end']) ? strtotime(date('Y-m-d 23:59:59',TIMESTAMP)): strtotime($_P['createtime']['end']) ;
    $res = Yxgifts_mod::getGiftsByInfo($themeid,'','','',$status,$page_start,$page_size);
    foreach ($res['list'] as $key => $val){
        $res['list'][$key]['yx_gift_name'] = $gifts[$val['gid']]['yx_gift_name'];
        $res['list'][$key]['yx_gift_img'] = $gifts[$val['gid']]['yx_gift_img'];
        if(!empty($val['workerid'])){
            $r = Worker::getWokerName($system,$themeid,$val['workerid']);
            $list[$key]['workername']=$r['workername'];
            $list[$key]['teamname']=$r['teamname'];
            $list[$key]['companyname']=$r['companyname'];
        }else{
            $list[$key]['workername']='';
            $list[$key]['teamname']='';
            $list[$key]['companyname']='';
        }
    }
    $flag_list = $res['list'];
    $flag_total = $res['total'];
    include_once $this->template('web/yxgifts');
}

if ($op=='search'){
    if (empty($_P['createtime']['start'])){
        $start_day =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
        $start_day =  strtotime(date('Y-m-d 00:00:00',$start_day));
    }else{
        $start_day =  strtotime($_P['createtime']['start']);
    }
    $end_day = empty($_P['createtime']['end']) ? strtotime(date('Y-m-d 23:59:59',TIMESTAMP)): strtotime($_P['createtime']['end']) ;
    if ($search_data){
        $search_data = '%'.$search_data.'%';
    }

    if (checksubmit('export_submit', true)){
        $ex_page = max ( 1, intval ( $_P['ex_page'] ) );
        $pageSize = 2000;
        $page_start = ($ex_page-1)*$ex_page;

        $res = Yxgifts_mod::getGiftsByInfo($themeid,$search_data,$start_day,$end_day,$status,$page_start,$pageSize);
        $total = $res['total'];
        $pagesize = ceil($pageSize/$pageSize);
        $header = array(
            'id' => 'ID','gid' => '礼品名称',  'username' => '客户姓名',
            'mobile' => '客户手机号','nickname' => '微信昵称','status' => '状态',
            'created_at'=>'创建时间'
        );
        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
//        for ($j = 1; $j <= $pagesize; $j++) {

            if (!empty($res['list'])) {

                foreach ($res['list'] as $row) {
                    $row['entry_nickname']=str_replace('"',"",$row['entry_nickname']);
                    $row['entry_nickname']=str_replace(',',"",$row['entry_nickname']);

                    $row['nickname']=str_replace('"',"",$row['nickname']);
                    $row['nickname']=str_replace(',',"",$row['nickname']);
                    $row['gid'] = $gifts[$row['gid']]['yx_gift_name'];
                        switch ($row['status']){
                            case 1:
                                $row['status'] = '未转发朋友圈';
                                break;
                            case 11:
                                $row['status'] = '失效';
                                break;
                            case 2:
                                $row['status'] = '未填写客资';
                                break;
                            case 3:
                                $row['status'] = '待兑换';
                                break;
                            case 4:
                                $row['status'] = '待核销';
                                break;
                            default:
                                break;
                        }
                        unset($info);
                    foreach ($keys as $key) {
                        $data[] = $row[$key];
                    }
                    $user[] = implode("\t ,", $data) . "\t ,";
                    unset($data);
                }
                $html .= implode("\n", $user)."\n";
                unset($user);
            }
        //}
        //新版本导出合并
        $excel_total  =  ($total/$pageSize);
        $is_end = 0;
        if($ex_page>=$excel_total){
            $is_end = 1;
        }
        Util::WxJump($system,$html,'摇一摇粉丝礼品信息',$themeid,$is_end,$excel_total);
    }

    $res = Yxgifts_mod::getGiftsByInfo($themeid,$search_data,$start_day,$end_day,$status,$page_start,$page_size);
    $flag_list = $res['list'];
    $flag_total = $res['total'];
    include_once $this->template('web/yxgifts');
}
}
}